﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />

Type.registerNamespace('SCS');

SCS.ElementOverlayBehavior = function (element) {
    SCS.ElementOverlayBehavior.initializeBase(this, [element]);

    /* Fields
    *******************************************************************************************************/
    this._closeClickedDelegate = null;
    this._resizeDelegate = null;
    this._overlayEscapeDelegate = null;
    this._invokingReqDelegate = null;

    this._coverElemId = null;
    this._background = null;

    this._triggerShowingText = null;
    this._triggerHidingText = null;

    this._bgClass = null;
    this._displaying = false;
};

SCS.ElementOverlayBehavior.prototype = {

    /* Setup
    *******************************************************************************************************/

    initialize: function () {

        //Sys.Debug.fail("");

        SCS.ElementOverlayBehavior.callBaseMethod(this, 'initialize');

        this.set_ContextKey(this.get_element().getAttribute("contextKey"));

        this._resizeDelegate = Function.createDelegate(this, this._onResize);
        $addHandler(window, "resize", this._resizeDelegate);

        this._invokingReqDelegate = Function.createDelegate(this, this._onInvokingRequest);
        Sys.Net.WebRequestManager.add_invokingRequest(this._invokingReqDelegate);
    },

    /* Teardown
    *******************************************************************************************************/

    dispose: function () {

        $removeHandler(window, "resize", this._resizeDelegate);
        this._resizeDelegate = null;

        Sys.Net.WebRequestManager.remove_invokingRequest(this._invokingReqDelegate);
        this._invokingReqDelegate = null;

        SCS.ElementOverlayBehavior.callBaseMethod(this, 'dispose');
    },

    /* Override Methods
    *******************************************************************************************************/

    populate: function (contextKey) {

        this.toggleOverlay(contextKey);
    },

    /* Private Methods
    *******************************************************************************************************/

    toggleOverlay: function (contextKey) {

        var triggerElem = $get(this._populateTriggerID);

        if (this._displaying) {

            this.hideOverlay();
            this.restoreBackground();

            if (this._overlayEscapeDelegate)
                $removeHandler(document, "keypress", this._overlayEscapeDelegate);

            this._overlayEscapeDelegate = null;

            if (triggerElem && this._triggerHidingText)
                triggerElem.innerHTML = this._triggerHidingText;
        }
        else {
            this.showOverlay();
            this.washBackground();

            var key = (typeof (contextKey) == "undefined") ? this.get_ContextKey() : contextKey;

            SCS.ElementOverlayBehavior.callBaseMethod(this, 'populate', [contextKey]);

            this._overlayEscapeDelegate = Function.createDelegate(this, this._onEscape);
            $addHandler(document, "keypress", this._overlayEscapeDelegate);

            if (triggerElem && this._triggerShowingText)
                triggerElem.innerHTML = this._triggerShowingText;
        }

        this._displaying = !this._displaying;
    },

    showOverlay: function () {

        var coverElem = (this._coverElemId) ? $get(this._coverElemId) : document.body;

        if (!coverElem)
            return;

        var overlay = this.get_element();

        overlay.setAttribute("style", String.format("{0};", this._getCommonStyle()));

        this.raiseShown(Sys.EventArgs.Empty);
    },

    hideOverlay: function () {

        this.get_element().style.display = "none";

        this.raiseHidden(Sys.EventArgs.Empty);
    },

    washBackground: function () {

        if (!this._bgClass)
            return;

        var background = this._background;

        if (!background) {
            background = document.createElement("div");
            background.setAttribute("class", this._bgClass);

            document.body.appendChild(background);
        }

        background.setAttribute("style", String.format("{0};", this._getCommonStyle()));

        this._background = background;
    },

    restoreBackground: function () {

        if (this._background)
            this._background.style.display = "none";
    },

    _getCommonStyle: function () {

        var coverElem;
        var style = "";

        if (this._coverElemId) {
            coverElem = $get(this._coverElemId);

            var bounds = Sys.UI.DomElement.getBounds(coverElem);

            style = String.format("display:block; position:absolute; top:{0}px; left:{1}px; height:{2}px; width:{3}px",
                bounds.y, bounds.x, bounds.height, bounds.width);
        }
        else {
            coverElem = document.body;

            style = "display:block; position:fixed; top:0; left:0; height:100%; width:100%; margin:0;";
        }

        return style;
    },

    /* Event Handlers
    *******************************************************************************************************/

    _onResize: function () {

        if (this._displaying) {
            this.showOverlay();
            this.washBackground();
        }
    },

    _onEscape: function () {

        this.toggleOverlay();
    },

    _onInvokingRequest: function () {

        if (this._displaying) {
            this.hideOverlay();
            this.restoreBackground();
        }
    },

    /* Events
    *******************************************************************************************************/

    add_shown: function (handler) {

        this.get_events().addHandler('shown', handler);
    },
    remove_shown: function (handler) {

        this.get_events().removeHandler('shown', handler);
    },
    raiseShown: function (eventArgs) {

        var handler = this.get_events().getHandler('shown');
        if (handler) {
            handler(this, eventArgs);
        }
    },

    add_hidden: function (handler) {

        this.get_events().addHandler('hidden', handler);
    },
    remove_hidden: function (handler) {

        this.get_events().removeHandler('hidden', handler);
    },
    raiseHidden: function (eventArgs) {

        var handler = this.get_events().getHandler('hidden');
        if (handler) {
            handler(this, eventArgs);
        }
    },

    /* Event Scripts
    *******************************************************************************************************/

    get_ShownScript: function () {
        return this.get_events().getHandler("shown");
    },

    set_ShownScript: function (value) {

        var func = this._wireEvent(value);

        if (func)
            this.add_shown(func);
    },

    get_HiddenScript: function () {
        return this.get_events().getHandler("hidden");
    },

    set_HiddenScript: function (value) {

        var func = this._wireEvent(value);

        if (func)
            this.add_hidden(func);
    },

    get_PopulatedScript: function () {
        return this.get_events().getHandler("populated");
    },

    set_PopulatedScript: function (value) {

        var func = this._wireEvent(value);

        if (func)
            this.add_populated(func);
    },

    get_PopulatingScript: function () {
        return this.get_events().getHandler("populating");
    },

    set_PopulatingScript: function (value) {
        
        var func = this._wireEvent(value);

        if (func)
            this.add_populating(func);            
    },

    _wireEvent: function (script) {

        var func = null;

        if (script) {
            func = CommonToolkitScripts.resolveFunction(script);

            if (!func)
                this._throwArgEx(value);
        }

        return func;
    },

    _throwArgEx: function (value) {

        throw Error.argumentType('value', typeof (value), 'Function', 'resize handler not a function, function name, or function text.');
    },

    /* Properties
    *******************************************************************************************************/

    set_CoverElementId: function (value) {
        this._coverElemId = value;
    },
    get_CoverElementId: function () {
        return this._coverElemId;
    },

    set_TriggerShowingText: function (value) {
        this._triggerShowingText = value;
    },
    get_TriggerShowingText: function () {
        return this._triggerShowingText;
    },

    set_TriggerHidingText: function (value) {
        this._triggerHidingText = value;
    },
    get_TriggerHidingText: function () {
        return this._triggerHidingText;
    },

    set_BackgroundCssClass: function (value) {
        this._bgClass = value;
    },
    get_BackgroundCssClass: function () {
        return this._bgClass;
    }
};

/* Registration
*******************************************************************************************************/

SCS.ElementOverlayBehavior.registerClass('SCS.ElementOverlayBehavior', Sys.Extended.UI.DynamicPopulateBehavior);
